package com.cybertron.ironhide.manage.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;

public class ImageDownLoadThreadUtil extends Thread {

	private final String imgUrl;

	private final String filePath;

	public ImageDownLoadThreadUtil(String imgUrl, String filePath) {
		this.imgUrl = imgUrl;
		this.filePath = filePath;
	}

	@Override
	public void run() {
		try {
			BufferedInputStream in = new BufferedInputStream(
					new URL(imgUrl).openStream());
			File img = new File(filePath);
			File parentFile = img.getParentFile();
			if (parentFile.exists() == false) {
				parentFile.mkdirs();
			}
			BufferedOutputStream out = new BufferedOutputStream(
					new FileOutputStream(img));
			byte[] buf = new byte[2048];
			int length = in.read(buf);
			while (length != -1) {
				out.write(buf, 0, length);
				length = in.read(buf);
			}
			in.close();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
